package TreeHole.mod;

import basemod.*;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.evacipated.cardcrawl.modthespire.lib.SpireConfig;
import com.megacrit.cardcrawl.core.CardCrawlGame;
import com.megacrit.cardcrawl.core.Settings;
import com.megacrit.cardcrawl.helpers.FontHelper;
import com.megacrit.cardcrawl.helpers.ImageMaster;
import com.megacrit.cardcrawl.localization.UIStrings;
import com.megacrit.cardcrawl.screens.options.DropdownMenu;
import com.megacrit.cardcrawl.screens.options.DropdownMenuListener;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Properties;
import java.util.function.BiConsumer;

public class ModConfig {

    public static final String TREE_HOLE_COUNT_LOWER = "treehole:TREE_HOLE_COUNT_LOWER";
    public static final String TREE_HOLE_COUNT_UPPER = "treehole:TREE_HOLE_COUNT_UPPER";

    public static int countLower;
    public static int countUpper;

    public static ArrayList<String> options;
    public static UIStrings uiStrings;

    public static SpireConfig config = null;
    private static Properties defaultSettings = new Properties();
    private static ModPanel settingsPanel;

    public static int getCountLower(){
        if(BaseMod.hasModID("spireTogether:"))
            return 3;
        return countLower;
    }

    public static int getCountUpper(){
        if(BaseMod.hasModID("spireTogether:"))
            return 4;
        return countUpper;
    }

    public static void initModSettings(){
        defaultSettings.setProperty(TREE_HOLE_COUNT_LOWER,String.valueOf(2));
        defaultSettings.setProperty(TREE_HOLE_COUNT_UPPER,String.valueOf(3));

        try {
            config = new SpireConfig("TreeHoleMod","Common",defaultSettings);
            config.load();
            countLower = config.getInt(TREE_HOLE_COUNT_LOWER);
            countUpper = config.getInt(TREE_HOLE_COUNT_UPPER);
        }
        catch (Exception e){

        }
    }

    public static void initModConfigMenu(){
        uiStrings = CardCrawlGame.languagePack.getUIString("treehole:Config");
        options = new ArrayList<>();
        for (int i =0;i<=6;i++){
            options.add(String.valueOf(i));
        }

        settingsPanel = new ModPanel();
        addEnableMenu();
        String modConfDesc = uiStrings.TEXT[0];
        Texture badge  = ImageMaster.loadImage("TreeHoleResources/img/icons/treeHoleLogo.png");
        BaseMod.registerModBadge(badge,"treehole","FimmlpS",modConfDesc,settingsPanel);
    }

    private static void addEnableMenu(){
        ModDropDownMenu drop = new ModDropDownMenu(380F,720F,(i,s)->{
            countLower = i;
            config.setInt(TREE_HOLE_COUNT_LOWER,countLower);
            try {
                config.save();
            }
            catch (IOException e){

            }
        }, new ArrayList<>(options), FontHelper.charDescFont,Settings.CREAM_COLOR,uiStrings.TEXT[1]);
        drop.menu.setSelectedIndex(countLower);

        ModDropDownMenu drop2 = new ModDropDownMenu(380F,635F,(i,s)->{
            countUpper = i;
            config.setInt(TREE_HOLE_COUNT_UPPER,countUpper);
            try {
                config.save();
            }
            catch (IOException e){

            }
        }, new ArrayList<>(options),FontHelper.charDescFont,Settings.CREAM_COLOR,uiStrings.TEXT[2]);
        drop2.menu.setSelectedIndex(countUpper);


        settingsPanel.addUIElement(drop2);
        settingsPanel.addUIElement(drop);

    }


    public static class ModDropDownMenu implements IUIElement, DropdownMenuListener {
        private float xPos;
        private float yPos;
        private final BiConsumer<Integer,String> onChangeSelectionTo;
        private final DropdownMenu menu;
        private final ModLabel textLabel;
        public float renderLayer;

        public ModDropDownMenu(float x, float y, BiConsumer<Integer,String> listener, ArrayList<String> options, BitmapFont font, Color textColor, String labelText){
            onChangeSelectionTo = listener;
            menu = new DropdownMenu(this,options,font,textColor);
            xPos = x;
            yPos = y;
            renderLayer = 1;
            this.textLabel = new ModLabel(labelText,xPos,yPos+20F,textColor,font,ModConfig.settingsPanel,(modLabel -> {}));
        }

        @Override
        public void render(SpriteBatch sb){
            sb.setColor(Color.WHITE);
            menu.render(sb,xPos* Settings.xScale,yPos*Settings.yScale);
            this.textLabel.render(sb);
        }

        @Override
        public void update() {
            menu.update();
            textLabel.update();
        }

        @Override
        public int renderLayer() {
            return 1;
        }

        @Override
        public int updateOrder() {
            return 1;
        }

        @Override
        public void changedSelectionTo(DropdownMenu dropdownMenu, int i, String s) {
            this.onChangeSelectionTo.accept(i,s);
        }

        @Override
        public void set(float xPos, float yPos) {
            this.xPos = xPos;
            this.yPos = yPos;
        }

        @Override
        public float getX() {
            return this.xPos;
        }

        @Override
        public void setX(float xPos) {
            this.xPos = xPos;
            this.textLabel.setX(xPos);
        }

        @Override
        public float getY() {
            return this.yPos;
        }

        @Override
        public void setY(float yPos) {
            this.yPos = yPos;
            this.textLabel.setY(yPos + 20.0F);
        }
    }
}
